package com.example.PolimiAssistant.adapters;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.Wsdl2Code.WebServices.PolimiAssistant.CourseExamInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.example.PolimiAssistant.R;

/**
 * ProfileCourseListAdapter.java
 *
 * This class is an adapter used to populate the course list in the profile
 * activity. When the UI needs to populate the list dynamically, this method
 * will tell the UI how to translate the arrived data into the view.
 */
public class ProfileCourseListAdapter extends BaseExpandableListAdapter{


    /**
     * Map that holds the group names with their corresponding child list
     */
    private Map<String, List<CourseExamInfo>> childList;

    /**
     * The list of the group names
     */
    private ArrayList<String> parentList;

    /**
     * The layout inflater to dynamically allocate the layout
     */
    private LayoutInflater layoutInflater;

    public ProfileCourseListAdapter(ArrayList<String> parents,
                                    Map<String, List<CourseExamInfo>> childs) {
        childList = childs;
        parentList = parents;
    }

    public void setInflater(LayoutInflater inflater) {
        layoutInflater = inflater;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {

        //1. Set the view to use the predefine item layout
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.profile_courses_list_item, null);
        }

        //2. Extract the item description or object
        CourseExamInfo course;
        course = childList.get(parentList.get(groupPosition)).get(childPosition);

        //3. Extract the item view to modify
        TextView courseName;
        TextView courseCode;
        TextView courseMark;

        courseName = (TextView) convertView.findViewById(R.id.profile_active_courses_item_name);
        courseCode = (TextView) convertView.findViewById(R.id.profile_active_courses_item_code);
        courseMark = (TextView) convertView.findViewById(R.id.profile_active_courses_item_mark);


        //4. Insert data for the item view
        courseName.setText(course.course.name);
        courseCode.setText(Integer.toString(course.course.id));
        courseMark.setText(Double.toString(course.grade));

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        //1. Set the view to use the predefine group layout
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.profile_courses_list_group, null);
        }

        //2. Extract the group view to modify
        TextView groupName;
        groupName = (TextView) convertView.findViewById(R.id.profile_active_courses_title);

        //3. Insert data for the group view
        groupName.setText(parentList.get(groupPosition));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(parentList.get(groupPosition)).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public CourseExamInfo getChild(int groupPosition, int childPosition) {
        return childList.get(parentList.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}
